package Homework;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class CountFilesNumber {

	public static void main(String[] args) {
		String path = "d://unity project";
		countByFileType(getAllFiles(path));
	}

	private static void countByFileType(List<File> allFiles) {
		Map<String, Long> collect = allFiles.stream().map(File::getName).filter(c->c.contains(".")).map(c->c.split("\\.")[1]).collect(Collectors.groupingBy(f->f,Collectors.counting()));
		collect.entrySet().stream().filter(c->!c.getKey().equals("")).sorted((e1,e2)->e1.getKey().charAt(0)-e2.getKey().charAt(0)).forEach(e->System.out.printf("%s::%d个\n", e.getKey(),e.getValue()));
	}

	private static List<File> getAllFiles(String path) {
		File rootDir = new File(path);
		File[] listFiles = rootDir.listFiles();
		List<File> allFiles = Arrays.stream(listFiles).filter(File::isFile).collect(Collectors.toList());
		Arrays.stream(listFiles).filter(File::isDirectory).map(File::getAbsolutePath).forEach(c->allFiles.addAll(getAllFiles(c)));
		return allFiles;
	}		

}
